package com.prevpn.api;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.prevpn.common.CommonResponse;
import com.prevpn.common.CommonUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * RoleApi - 调用 aTrust OpenAPI 角色接口
 */
@Component
public class RoleApi {

    /**
     * 调用 aTrust 新增角色接口
     */
    public CommonResponse<Map<String, Object>> createRole(String directoryDomain,
                                                          String name,
                                                          String description,
                                                          String externalId) {
        String path = "/api/v3/role/create";

        // 构造请求体
        Map<String, Object> body = new HashMap<>();
        body.put("directoryDomain", directoryDomain);
        body.put("name", name);
        if (description != null) body.put("description", description);
        if (externalId != null) body.put("externalId", externalId);

        // 调用 OpenAPI
        String response = CommonUtil.openApiPostRequest(path, JSONUtil.toJsonStr(body));

        // 转换响应对象
        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {},
                false);
    }

    /**
     * 调用 aTrust 查询角色详情接口（基于角色名称）
     */
    public CommonResponse<Map<String, Object>> queryRoleByName(String directoryDomain,
                                                               String name,
                                                               Integer type) {
        String path = "/api/v3/role/queryByName";

        // 组装 GET 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("directoryDomain", directoryDomain);
        params.put("name", name);
        if (type != null) params.put("type", type);

        // 调用 OpenAPI
        String response = CommonUtil.openApiGetRequest(path, params);

        // 转换 JSON 响应
        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {},
                false);
    }

    /** 角色授权资源接口路径 */
    private static final String ASSIGN_RESOURCE_PATH = "/api/v3/role/assignResourceByName";

    /**
     * 调用 aTrust 接口 - 角色分配资源（基于名称）
     */
    public CommonResponse<Map<String, Object>> assignResourceByName(String jsonBody) {
        String response = CommonUtil.openApiPostRequest(ASSIGN_RESOURCE_PATH, jsonBody);
        return JSONUtil.toBean(
                response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {},
                false
        );
    }


    /**
     * 查询角色列表（分页 + 模糊搜索）
     */
    public CommonResponse<Map<String, Object>> queryRoleList(Map<String, Object> body) {
        String path = "/api/v3/role/queryAll";
        String response = CommonUtil.openApiPostRequest(path, JSONUtil.toJsonStr(body));
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    // 批量删除角色
    private static final String BULK_DELETE_BY_NAME_LIST_PATH = "/api/v3/role/bulkDeleteByNameList";

    /**
     * 调用 OpenAPI 批量删除角色
     */
    public CommonResponse<Map<String, Object>> bulkDeleteByNameList(String jsonBody) {
        String response = CommonUtil.openApiPostRequest(BULK_DELETE_BY_NAME_LIST_PATH, jsonBody);
        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    /**
     * 编辑角色（基于名称）
     */
    private static final String UPDATE_ROLE_BY_NAME_PATH = "/api/v3/role/updateByName";
    public CommonResponse<Map<String, Object>> updateRoleByName(String jsonBody) {
        String response = CommonUtil.openApiPostRequest(UPDATE_ROLE_BY_NAME_PATH, jsonBody);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }
}
